#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>

int main() {
    // 打开JPEG图像文件
    AVFormatContext *format_context = avformat_alloc_context();
    if (avformat_open_input(&format_context, "test.jpg", NULL, NULL) != 0) {
        printf("Error opening image file\n");
        return -1;
    }
    if (avformat_find_stream_info(format_context, NULL) < 0) {
        printf("Error finding stream information\n");
        return -1;
    }

    // 查找并打开解码器
    AVCodec *codec = NULL;
    int stream_index = av_find_best_stream(format_context, AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0);
    if (stream_index < 0) {
        printf("Error finding video stream\n");
        return -1;
    }
    AVCodecContext *codec_context = avcodec_alloc_context3(codec);
    if (!codec_context) {
        printf("Error allocating codec context\n");
        return -1;
    }
    if (avcodec_parameters_to_context(codec_context, format_context->streams[stream_index]->codecpar) < 0) {
        printf("Error setting codec parameters\n");
        return -1;
    }
    // 获取帧格式
    AVCodecParameters *codecParameters = format_context->streams[stream_index]->codecpar;
    enum AVPixelFormat pixelFormat = codecParameters->format;
    const char *pixelFormatName = av_get_sample_fmt_name(pixelFormat);
    printf("帧格式: %d \n", pixelFormat);
//exit(1);
    if (avcodec_open2(codec_context, codec, NULL) < 0) {
        printf("Error opening codec\n");
        return -1;
    }

    // 解码图像帧
    AVPacket packet;
    av_init_packet(&packet);
    while (av_read_frame(format_context, &packet) >= 0) {
        if (packet.stream_index == stream_index) {
            int ret = avcodec_send_packet(codec_context, &packet);
            if (ret < 0) {
                printf("Error sending a packet for decoding\n");
                break;
            }
            
            AVFrame *frame = av_frame_alloc();
            int idx = 0;
            while (ret >= 0) {
                ret = avcodec_receive_frame(codec_context, frame);
                if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                    break;
                else if (ret < 0) {
                    printf("Error during decoding\n");
                    break;
                }

                idx++;
                // 打印YUV数据
                int max = 0, ax, ay;
                int height = frame->height;
                int width = frame->width;
                for (int y = 0; y < height; y++) {
                    uint8_t *y_ptr = frame->data[0] + y * frame->linesize[0];
                    uint8_t *u_ptr = frame->data[1] + (y / 2) * frame->linesize[1];
                    uint8_t *v_ptr = frame->data[2] + (y / 2) * frame->linesize[2];
                    for (int x = 0; x < width; x++) {
                        printf(" %d ", y_ptr[x]);
                        if (y_ptr[x] > max) {
                          max = y_ptr[x];
                          ax = x, ay = y;
                        }
                      //  printf("Y: %d, U: %d, V: %d\n", y_ptr[x], u_ptr[x / 2], v_ptr[x / 2]);
                    }
                  printf ("\n");
                }
                printf ("max:%d %d:%d\n", ax, ay, max);
                // 处理解码后的图像帧
                // frame->data[0], frame->data[1], frame->data[2] 分别对应 YUV420 格式的 Y, U, V 数据

                av_frame_unref(frame);
            }
            av_frame_free(&frame);
        }
        av_packet_unref(&packet);
    }

    // 释放资源
    avformat_close_input(&format_context);
    avformat_free_context(format_context);
    avcodec_free_context(&codec_context);

    return 0;
}

